package org.opengauss.datachecker.extract;

import org.opengauss.datachecker.common.exception.ExtractBootstrapException;
import org.opengauss.datachecker.common.service.CommonCommandLine.CmdOption;
import org.opengauss.datachecker.extract.cmd.ExtractCommandLine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Indexed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.opengauss.datachecker.extract.constants.ExtConstants.PROFILE_SINK;
import static org.opengauss.datachecker.extract.constants.ExtConstants.PROFILE_SOURCE;

/**
 * ExtractApplication
 *
 * author wang chao
 * date 2022/5/8 19:27
 * since 11
 **/
@Indexed
@EnableFeignClients(basePackages = {"org.opengauss.datachecker.extract.client"})
@SpringBootApplication
@ComponentScan(value = {"org.opengauss.datachecker.extract", "org.opengauss.datachecker.common"})
public class ExtractApplication {
    private static final Logger logger = LoggerFactory.getLogger(ExtractApplication.class);

    public static void main(String[] args) {
        logger.info("Starting ExtractApplication...");

        ExtractCommandLine commandLine = new ExtractCommandLine();
        logger.debug("Parsing command line arguments: {}", (Object) args);
        commandLine.parseArgs(args);

        SpringApplication application = new SpringApplication(ExtractApplication.class);
        String mode = commandLine.getMode();
        logger.debug("Setting system property {}: {}", CmdOption.LOG_NAME, mode);
        System.setProperty(CmdOption.LOG_NAME, mode);

        if (commandLine.hasOnlySink()) {
            logger.info("Starting application with profile: {}", PROFILE_SINK);
            application.setAdditionalProfiles(PROFILE_SINK);
            application.run(args);
        } else if (commandLine.hasOnlySource()) {
            logger.info("Starting application with profile: {}", PROFILE_SOURCE);
            application.setAdditionalProfiles(PROFILE_SOURCE);
            application.run(args);
        } else if (commandLine.hasHelp()) {
            logger.info("Displaying help information");
            commandLine.help();
        } else {
            logger.error("Invalid profile setting, displaying help information");
            commandLine.help();
            throw new ExtractBootstrapException("Extract profile setting error, profile must be 'sink' or 'source'");
        }
    }
}
